<template>
    <div>
        <c-header :p-title="'管理管理员'">
            <button class="btn btn-sm btn-primary m-r-sm" @click="importFile">导入人员文档</button>
            <button class="btn btn-sm btn-primary m-r-sm"  @click='editDetail()'>添加</button>
            <button type="button" class="btn btn-sm btn-info"  @click="$router.go(-1)"><i class="fa fa-mail-reply m-r-sm"></i>返回</button>
            <btn-refresh class='m-l-15' :p-data='init'></btn-refresh>
        </c-header>
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="panel-heading search-heading">
                            <small class="text-muted">搜索筛选项：</small>
                            <div class="m-b-sm m-t-sm">
                                <form class="clearfix" action="javascript:void(0)">
                                    <div class="pull-left m-r m-b-sm">
                                        <label class="control-label">角色：</label>
                                        <status-select class='thumb-search' :p-data="search.role_id" p-name="qxgly_role" :p-far='true'  @on-change='searchSelectRole'></status-select>
                                    </div>
                                    <div class="pull-left w100 text-right">
                                        <button class="btn btn-sm btn-info" type="button" @click="searchChange()">搜索</button>
                                        <Tooltip content="重置默认搜索项" placement="top">
                                            <button class="btn btn-sm btn-dark" type="button" @click="searchReset(true)">重置</button>
                                        </Tooltip>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <hr/>
                        <div class="table-responsive"  v-if='list'>
                            <table class="vm table color-bordered-table base-bordered-table table-bordered table-hover">
                                <thead>
                                <tr class="nowrap">
                                    <!--<th>ID</th>-->
                                    <th>姓名</th>
                                    <th>电话</th>
                                    <th>公司名称</th>
                                    <th>职位</th>
                                    <th>角色</th>
                                    <th>状态</th>
                                    <th width="60">操作</th>
                                </tr>
                                </thead>
                                <tbody  v-show='list.length'>
                                <tr v-for="(item,key) in list">
                                    <!--<td>{{item.id}}</td>-->
                                    <td>{{item.name}}</td>
                                    <td>{{item.phone}}</td>
                                    <td>{{item.organization}}</td>
                                    <td>{{item.position}}</td>
                                    <td><status-label :p-data="item.role_id" p-name='qxgly_role' :p-far='true'></status-label></td>
                                    <td><status-label :p-data="item.status"  p-name='worker_status' :p-far="true"></status-label></td>
                                    <td class="no-h-bg">
                                        <div class="table-edit-group">
                                            <Tooltip content="编辑" placement="top">
                                                <button class="btn btn-sm btn-success m-l-sm"  @click='editDetail(item,true)'><i class="fa fa-edit"></i></button>
                                            </Tooltip>
                                        </div>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <div  v-show='!list.length' class="text-center m-b-sm">
                                暂无数据！
                            </div>
                        </div>
                        <hr/>
                        <!--<footer class="panel-footer text-right">
                            <CPage :total="search.page_total"
                                   :current='search.page_current'
                                   :page-size='search.page_row'
                                   @on-change='pageCurrentChange'
                                   @on-page-size-change='pageRowChange'></CPage>
                        </footer>-->
                    </div>
                </div>
            </div>
        </div>
        <operate-save ref="operate_save" @on-change="initSubmit"></operate-save>
        <operate-import ref="operate_import" @on-change="initSubmit"></operate-import>
    </div>
</template>
<script>
    import serWorkerPerson from '../../serves/api-worker-person'
    import operateSave from './save'
    import operateImport from './OperateImport.vue'
    const __page_key = 'worker.person.list';
    let searchDef = {
        role_id:'',

        page_row:'',
        page_current:'',
        page_total:''
    };
    export default{
        components:{
            operateSave,
            operateImport,
        },
        data(){
            return{
                list:undefined,
                search:{}
            }
        },
        computed:{
            cDate(){
                return [this.search.start_at,this.search.end_at]
            },
        },
        beforeMount(){
            this.searchReset();
        },
        mounted(){
            this.init();
        },
        methods:{
            searchReset(update,pageState){
                if(pageState){
                    this.$clientPage.def(this.search);
                    return false
                }
                const _p=this.$clientPage.getData(__page_key);
                if(_p){
                    this.$set(this,'search',_p);
                }else{
                    this.$set(this,'search',Object.assign({},searchDef));
                    this.$clientPage.def(this.search);
                }
                if(update){
                    //this.init();
                }
            },
            searchPage(page){
                this.$clientPage.set(this.search,page);
            },
            searchChange(){
                this.$clientPage.def(this.search);
                this.init();
            },
            pageRowChange(num){
                if(num!= this.search.page_row){
                    this.$set(this.search,'page_row',num);
                    if(this.search.page_current == 1){
                        this.init();
                    }
                }
            },
            pageCurrentChange(current){
                if(current!= this.search.page_current){
                    this.$set(this.search,'page_current',current);
                    this.init();
                }
            },
            linkPageChange(name,params,query){
                this.$clientPage.setData(__page_key,Object.assign({},this.search));
                if(name){
                    this.$router.push({name:name,params:params,query:query})
                }
            },
            //------------------------------------------------------------------------
            async init(data){
                let res = await serWorkerPerson.list({
                    role_id:this.search.role_id
                });
                if(res.code==1){
                    this.list = res.data||[];
                    //this.searchPage(res.addtion_data&&res.addtion_data.page_info);
                }else{
                    this.list = [];
                    this.searchReset(undefined,true);
                    this.$Message.error(res.message);
                }
                return true;
            },
            searchSelectRole(value){
                this.$set(this.search,'role_id',value);
            },
            editDetail(data,edit){
                this.$refs['operate_save'].handleOpen(Object.assign({_type:edit?'edit':'add'},data))
            },
            initSubmit(state){
                if(state){
                    this.init()
                }
            },
            importFile(){
                this.$refs['operate_import'].handleOpen({})
            },
        }
    }
</script>